using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using SMS.RoamSMPP.SmppPdu;
using WorkThreading;
using digit.Core;

namespace SMS.Gateway.Libs
{
    public class MsgInWorkItem : WorkItem
    {
        // Fields
        private string _messageInfo;
        private int _requestId;
        private string _serviceId;
        private string _userId;
        private Gateway frmGateway;
        private static Dictionary<string, string> _servDic;

        // Methods
        static MsgInWorkItem()
        {
            _servDic = new Dictionary<string, string>();
            _servDic.Add("8078", "80");
            _servDic.Add("8178", "81");
            _servDic.Add("8278", "82");
            _servDic.Add("8378", "83");
            _servDic.Add("8478", "84");
            _servDic.Add("8578", "85");
            _servDic.Add("8678", "86");
            _servDic.Add("8778", "87");
        }

        public MsgInWorkItem(SmppDeliverSmReq MO, Gateway frm)
        {
            this._userId = MO.Body.SourceAddress;
            this._serviceId = MO.Body.DestinationAddress;
            this._messageInfo = MO.Body.ShortMessage.ToString();
            this._requestId = MO.Header.SequenceNumber;
            this.frmGateway = frm;
        }

        public MsgInWorkItem(string UserID, string ServiceID, string MessageInfo, int RequestID, Gateway frm)
        {
            this._userId = UserID;
            this._serviceId = ServiceID;
            this._messageInfo = MessageInfo;
            this._requestId = RequestID;
            this.frmGateway = frm;
        }

        private void InsertMsgIn()
        {
            if (this._messageInfo.Trim() != "")
            {
                string connStr = ConfigApp.ConnStr;
                string _servId = "";

                if (this._serviceId.Length > 3)
                    this._serviceId = this._serviceId.Substring(this.ServiceID.Length - 4);

                if (!_servDic.ContainsKey(_serviceId))
                    throw new Exception("RequestID " + this._requestId.ToString() + " Invalid ServiceID = " + this._serviceId);

                _servId = _servDic[_serviceId];

                string cmdText = ("INSERT INTO MsgIn_" + _servId + "(RequestID,MobileNo,ServiceID,MsgContent)") + " VALUES (@RequestID,@UserID,@ServiceID,@Message)";

                SqlConnection cnn = new SqlConnection(connStr);
                SqlCommand cmd = new SqlCommand(cmdText, cnn);
                cmd.Parameters.Clear();

                cmd.Parameters.Add("@RequestID", SqlDbType.Int).Value = this._requestId;
                cmd.Parameters.Add("@UserID", SqlDbType.VarChar, 20).Value = this._userId;
                cmd.Parameters.Add("@ServiceID", SqlDbType.VarChar, 50).Value = this._serviceId;
                cmd.Parameters.Add("@Message", SqlDbType.VarChar, 200).Value = this._messageInfo;

                cnn.Open();
                cmd.ExecuteNonQuery();
                cnn.Close();
            }
        }

        public override void Perform()
        {
            try
            {
                lock (this.frmGateway.SmppClient)
                {
                    this.frmGateway.SmppClient.DeliverSmRes(new SmppDeliverSmRes(this._requestId, ""));
                }
                this.InsertMsgIn();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        // Properties
        public string MessageInfo
        {
            get
            {
                return this._messageInfo;
            }
            set
            {
                this._messageInfo = value;
            }
        }

        public int RequestID
        {
            get
            {
                return this._requestId;
            }
            set
            {
                this._requestId = value;
            }
        }

        public string ServiceID
        {
            get
            {
                return this._serviceId;
            }
            set
            {
                this._serviceId = value;
            }
        }

        public string UserID
        {
            get
            {
                return this._userId;
            }
            set
            {
                this._userId = value;
            }
        }
    }
}

